/*
 * Copyright (c) 2009 by unreserved.org.
 * All rights reserved.
 */

package org.unreserved.ladderplot;

import java.util.*;
import org.apache.log4j.Logger;

/**
 * Internal representation of a LadderPlot file, that is, the model
 * encompasses players, messages, annotations etc.
 */
public class Model implements Container {
    private static final Logger logger = Logger.getLogger(Model.class);
    
    /** List of Player objects. */
    protected ArrayList<Player> players = new ArrayList<Player>();
    
    /** Left margin treated as a special Player at column -1. */
    protected final Player leftMargin = new Player("^", "^"/*, -1*/);
    
    /** List of Message and Annotation objects. */
    protected ArrayList<Event> events = new ArrayList<Event>();
    
    protected int startLine = 0;
    protected int startColumn = 5;
    protected int linePadding = 0;
    protected int columnPitch = 12;
    protected boolean topLabel = true;
    protected boolean bottomLabel = false;
    protected boolean messageNumbering = false;
    protected boolean noteNumbering = false;
    
    
    ///////////////////////////////////////////////////////////////////////
    // setters and getters for options
    
    public void setStartLine(int startLine) {
        this.startLine = startLine;
    }
    
    public int getStartLine() {
        return startLine;
    }
    
    public void setStartColumn(int startColumn) {
        this.startColumn = startColumn;
    }
    
    public int getStartColumn() {
        return startColumn;
    }
    
    public void setLinePadding(int linePadding) {
        this.linePadding = linePadding;
    }
    
    public int getLinePadding() {
        return linePadding;
    }
    
    public void setColumnPitch(int columnPitch) {
        this.columnPitch = columnPitch;
    }
    
    public int getColumnPitch() {
        return columnPitch;
    }
    
    public void setTopLabel(boolean b) {
        this.topLabel = b;
    }
    
    public boolean getTopLabel() {
        return topLabel;
    }
    
    public void setBottomLabel(boolean b) {
        this.bottomLabel = b;
    }
    
    public boolean getBottomLabel() {
        return bottomLabel;
    }
    
    public void setMessageNumbering(boolean b) {
        this.messageNumbering = b;
    }
    
    public boolean getMessageNumbering() {
        return messageNumbering;
    }
    
    public void setNoteNumbering(boolean b) {
        this.noteNumbering = b;
    }
    
    public boolean getNoteNumbering() {
        return noteNumbering;
    }
    
    
    ///////////////////////////////////////////////////////////////////////
    
    public Player addPlayer(String name, String nickname) {
        logger.debug("addPlayer: " + nickname + " (" + name + ")");
        if (getPlayer(nickname) != null) {
            throw new IllegalArgumentException("Player with nickname \"" + nickname + "\" already exists");
        }
        Player p = new Player(name, nickname);
        if (players.size() == 0) {
            p.setColumn(startColumn);
        } else {
            Player prevPlayer = players.get(players.size() - 1);
            p.setColumn(prevPlayer.getColumn() + columnPitch);
        }
        players.add(p);
        return p;
    }
    
    public Player getPlayer(String nickname) {
        if ("^".equals(nickname)) {
            return leftMargin;
        }
        for (Player p : players) {
            if (nickname.equals(p.getNickname())) {
                return p;
            }
        }
        return null;
    }
    
    /** Returns list of Players (leftMargin not included). */
    public Player[] getPlayers() {
        return (Player[]) players.toArray(new Player[0]);
    }
    
    public void addEvent(Event ev) {
        logger.debug("addEvent: " +ev);
        events.add(ev);
    }
    
    public List<Event> getEvents() {
        return events;
    }
}
